package com.yegs.jltqc.serviceImpl;

import com.yegs.jltqc.TO.NamePageQuery;
import com.yegs.jltqc.VO.BaseUser;
import com.yegs.jltqc.common.StoreException;
import com.yegs.jltqc.config.ConcurentCache;
import com.yegs.jltqc.dao.BaseUserRepository;
import com.yegs.jltqc.service.BaseUserService;
import org.apache.tomcat.jni.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.yegs.jltqc.common.Status.STATUS_200;

@Service
public class BaseUserServiceImpl implements BaseUserService {

    @Autowired
    private BaseUserRepository baseUserRepository;

    @Override
    public void save(BaseUser baseUser) throws StoreException{
        if(baseUser.getId() == null){
            BaseUser user = baseUserRepository.findBaseUserByUsername(baseUser.getUsername());
            if(user != null){
                throw new StoreException("用户名已存在!");
            }
            user = baseUserRepository.findBaseUserByCode(baseUser.getCode());
            if(user != null){
                throw new StoreException("账号已存在!");
            }
            baseUser.setCreateBy(ConcurentCache.getUserName());
            baseUser.setCreateTime(new Date());
            baseUserRepository.save(baseUser);
        }else{
            BaseUser user = baseUserRepository.getOne(baseUser.getId());
            if(user == null){
                throw new StoreException("系统错误!");
            }
            BaseUser user1 = baseUserRepository.findBaseUserByUsername(baseUser.getUsername());
            if(user1 != null && user.getId() != user1.getId()){
                throw new StoreException("用户名已存在!");
            }
            user1 = baseUserRepository.findBaseUserByCode(baseUser.getCode());
            if(user1 != null  && user.getId() != user1.getId()){
                throw new StoreException("账号已存在!");
            }
            user.setCode(baseUser.getCode());
            user.setMenus(baseUser.getMenus());
            user.setStatus(baseUser.getStatus());
            user.setDepartmentName(baseUser.getDepartmentName());
            baseUserRepository.save(user);
        }
    }

    @Override
    public List<BaseUser> list() {
        return baseUserRepository.findAll();
    }

    @Override
    public BaseUser get(Long id) {
        return baseUserRepository.getOne(id);
    }

    @Override
    public void del(Long id) {
        baseUserRepository.deleteById(id);
    }

    @Override
    public BaseUser findByUsernameAndPassword(String username, String password) {
        return baseUserRepository.findBaseUserByUsernameAndPassword(username, password);
    }

    @Override
    public BaseUser findByCodeAndPassword(String code, String password) {
        return baseUserRepository.findBaseUserByCodeAndPassword(code,password);
    }

    @Override
    public Page<BaseUser> query(NamePageQuery query) {
        PageRequest request = PageRequest.of(query.getPageIndex() - 1, query.getPageSize());
        if(query.getName() == null || query.getName().trim() == ""){
            return baseUserRepository.findAll(request);
        }
        return baseUserRepository.findBaseUserByUsername(query.getName(), request);
    }
}


